import moment from 'moment';
// 把moment字段转换为字符串
export const submitFieldsAdaptor = (formvalue: any) => {
  const result = formvalue;
  Object.keys(formvalue).forEach((key) => {
    if (moment.isMoment(formvalue[key])) {
      result[key] = moment(formvalue[key]).format();
    }
    if (Array.isArray(result[key])) {
      result[key] = result[key].map((innerValue: any) => {
        if (moment.isMoment(innerValue)) {
          return moment(innerValue).format();
        }
        return innerValue;
      });
    }
  });
  return result;
};
// 处理moment时间
export const setFieldsAdaptor = (data: BasicListApi.PageData) => {
  const result = {};
  if (data?.layout?.tabs && data?.dataSource) {
    data.layout.tabs.forEach((tab) => {
      tab.data.forEach((item) => {
        switch (item.type) {
          case 'datetime':
            result[item.key] = moment(data.dataSource[item.key]);
            break;
          default:
            result[item.key] = data.dataSource[item.key];
            break;
        }
      });
    });
    return result;
  }
  return {};
};
